****************************************************************************
**Win or Lose: Residential Sorting After a School Choice Lottery
*By Andrew Bibler & Stephen B Billings

****************************************************************************

clear all 
set more off

use school_locations.dta , clear

gen curr_lead1 = real(SchoolID)

keep if ( curr_lead1>0 & curr_lead1<. )

drop if curr_lead1==386

rename X x_attend
rename Y y_attend

keep curr_lead1 x_attend y_attend

tempfile attschlloc
	save `attschlloc' 

rename curr_lead1 appch1school
rename x_attend x_appch1
rename y_attend y_appch1

tempfile appch1loc
	save `appch1loc' 

rename appch1school assschl
rename x_appch1 x_assschl
rename y_appch1 y_assschl

tempfile assschlloc
	save `assschlloc' 
	
use id_add_bg.dta , clear

forv x=1/9 {
	replace bg="`x'" if bg=="`x'0"
	}
	
gen bg_r = real(bg)

gen tract_r = real(tract) 

rename geo_addrid addrid_att
rename year_r year
rename blockgroup blockgroup_att
rename bg_r bg_att
rename tract_r tract_att

drop lea

tempfile bgatt
	save `bgatt'
		
rename addrid_att addrid_app
rename blockgroup_att blockgroup_app
rename bg_att bg_app 
rename tract_att tract_app

replace year = (year + 1)

tempfile bgapp
	save `bgapp' 
			
rename addrid_app addrid_preapp
rename blockgroup_app blockgroup_preapp
rename bg_app bg_preapp 
rename tract_app tract_preapp

replace year = (year + 1)

tempfile bgpreapp
	save `bgpreapp' 	

use CBG_locations.dta , clear

gen blkgrp_s = regexs(3) if regexm(FIPS , "([0-9][0-9][0-9][0-9][0-9])([0-9][0-9][0-9][0-9][0-9][0-9])([0-9])")
gen tract_s = regexs(2) if regexm(FIPS , "([0-9][0-9][0-9][0-9][0-9])([0-9][0-9][0-9][0-9][0-9][0-9])([0-9])")

gen tract = real(tract_s)
gen blkgrp = real(blkgrp_s)

gen double x = real(x_coord)
gen double y = real(y_coord) 

rename tract tract_app
rename blkgrp bg_app
rename x x_app
rename y y_app

keep tract_app bg_app x_app y_app

gen cbgapplink = 2000 

tempfile bg2000locapp 
	save `bg2000locapp' 
		
rename tract_app tract_att
rename bg_app bg_att
rename x_app x_att
rename y_app y_att
rename cbgapplink cbgattlink 

tempfile bg2000locatt 
	save `bg2000locatt' 		
	
rename tract_att tract_preapp
rename bg_att bg_preapp
rename x_att x_preapp
rename y_att y_preapp
rename cbgattlink cbgpreapplink

tempfile bg2000locpreapp 
	save `bg2000locpreapp' 	

use CBG2010_locations.dta , clear

gen tract = real(TRACTCE10)
gen blkgrp = real(BLKGRPCE10)

gen double x = real(x_coord)
gen double y = real(y_coord) 

rename tract tract_app
rename blkgrp bg_app
rename x x_app
rename y y_app

keep tract_app bg_app x_app y_app

gen cbgapplink = 2010 

tempfile bg2010locapp 
	save `bg2010locapp' 
		
rename tract_app tract_att
rename bg_app bg_att
rename x_app x_att
rename y_app y_att
rename cbgapplink cbgattlink 

tempfile bg2010locatt 
	save `bg2010locatt' 	
			
rename tract_att tract_preapp
rename bg_att bg_preapp
rename x_att x_preapp
rename y_att y_preapp
rename cbgattlink cbgpreapplink

tempfile bg2010locpreapp 
	save `bg2010locpreapp' 	
	
use `bg2000locatt' , clear
append using `bg2010locatt' 

tempfile bglocatt
	save `bglocatt' 
	
use `bg2000locapp' , clear
append using `bg2010locapp'

tempfile bglocapp
	save `bglocapp' 
		
use `bg2000locpreapp' , clear
append using `bg2010locpreapp'

tempfile bglocpreapp
	save `bglocpreapp' 		
		
use all-lottery-movers-analysis.dta , clear 

keep if sixsample

gen cbgattlink = .
replace cbgattlink = 2000 

gen cbgapplink = . 
replace cbgapplink = 2000 

gen cbgpreapplink = . 
replace cbgpreapplink = 2000 

merge m:1 curr_lead1 using `attschlloc' , gen(attschllocmerge)

drop if attschllocmerge==2

merge m:1 appch1school using `appch1loc' , gen(appch1locmerge)

drop if appch1locmerge==2

merge m:1 assschl using `assschlloc' , gen(assschllocmerge)

drop if assschllocmerge==2

merge 1:1 mastid year using `bgatt' , gen(bgattmerge)

drop if bgattmerge==2

merge m:1 tract_att bg_att cbgattlink using `bglocatt' , gen(bglocattmerge)

drop if bglocattmerge==2

merge 1:1 mastid year using `bgapp' , gen(bgappmerge)

drop if bgappmerge==2

merge m:1 tract_app bg_app cbgapplink using `bglocapp' , gen(bglocappmerge)

drop if bglocappmerge==2

merge 1:1 mastid year using `bgpreapp' , gen(bgpreappmerge)

drop if bgpreappmerge==2

merge m:1 tract_preapp bg_preapp cbgpreapplink using `bglocpreapp' , gen(bglocpreappmerge)

drop if bglocpreappmerge==2
	
save sixsample-lottery-movers-analysis-cbgs.dta , replace




